package com.holly.common_view.dialog;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.NumberPicker;
import android.widget.TextView;


import com.holly.common_view.R;

import java.util.List;

/**
 * 创建人 wzj 创建时间 2016/12/13.
 */

public class BottomPickerDialog {
    private Context context;
    private String[] dataString = null;
    private ViewGroup decorView;//activity的根View
    private NumberPicker dataView;
    private int resultValue ;
    private View view;
    private OnConfirmeListener confirmeListener;
    public interface OnConfirmeListener{
        void result(String res);
    }
    public BottomPickerDialog(Context context, List<String> list){
        this.context = context;
        confirmeListener = (OnConfirmeListener)context;
        dataString = new String[list.size()];
        for(int i=0;i<list.size();i++){
            dataString[i] = list.get(i);
        }
        initView();
    }

    private void initView() {
        LayoutInflater inflater = LayoutInflater.from(context);
        decorView = (ViewGroup) ((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content);
        view = inflater.inflate(R.layout.alertview_data,decorView,false);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
        params.gravity = Gravity.BOTTOM;
        view.setLayoutParams(params);
        dataView = (NumberPicker) view.findViewById(R.id.testdata);
        dataView.setDisplayedValues(dataString);
        dataView.setMinValue(0);
        dataView.setMaxValue(dataString.length
                - 1);
        dataView.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
            @Override
            public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
                resultValue = newVal ;
            }
        });
        TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
        view.findViewById(R.id.btnSubmit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                try {
                    confirmeListener.result(dataString[resultValue]);
                } catch (Exception e) {

                }
            }
        });
        view.findViewById(R.id.btnCancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        tvTitle.setText("请选择选项");
    }
    public void dismiss() {
        //从activity根视图移除
        decorView.removeView(view);
    }
    /**
     * 添加这个View到Activity的根视图
     */
    public void show() {
        if (isShowing()) {
            return;
        }
        decorView.addView(view);
    }
    /**
     * 检测该View是不是已经添加到根视图
     *
     * @return 如果视图已经存在该View返回true
     */
    public boolean isShowing() {
        View view = decorView.findViewById(R.id.outmost_container);
        return view != null;
    }
}
